*Get samples of states from each non-propensity-score-weighted regression 

clear
use "1850_1860_merged.dta"

*Sample for mobility by parental real estate

forvalues i = 1(1)2{
preserve


keep if pargroup2==`i'
drop if realprop_parents1850==.

keep if age1850>=16 & age1850<=18

tab age1850
tab age1860
count
#delimit ;
local vars occschool1860 constant;
foreach x of local vars{;
gen `x'wt = `x'*weight;
drop `x';
rename `x'wt `x';
};

#delimit ;
collapse (sum) occschool1860 constant (mean)statefip1850 hasnormalschool, by(cty_fips1850 white1850);

#delimit cr
drop if white1850==0

gen constantrd = round(constant,1)
gen below10 = constantrd<10
bysort statefip hasnormalschool: egen totctytype = count(hasnormalschool)
bysort statefip hasnormalschool: egen totbelow10 = total(below10)
gen fracbelow10 = totbelow10/totctytype
bysort statefip: egen maxfracbelow10 = max(fracbelow10)

*Drop states for which 50% or more of their normal or asylum counties have sample size less than 10

drop if maxfracbelow10>=.5

local vars occschool1860
foreach x of local vars{
gen `x'mean = `x'/constant
}

sort cty_fips

local vars occschool1860
foreach x of local vars{

reghdfe `x'mean hasnormalschool if white1850==1, absorb(statefip1850) vce(robust)
gen sampec`i'white1850x = e(sample)==1

local vars2 sampec`i'white1850
foreach y of local vars2{
bysort cty_fips: egen `y' = max(`y'x)
}

drop if white1850==0 

keep cty_fips sampec`i'white1850
save sampec`i'1850.dta, replace
}
#delimit cr
restore
}


********************************
*Sample for occupations and parental real estate distribution
********************************

preserve

drop if realprop_parents1850==.

keep if age1850>=16 & age1850<=18

tab age1850
tab age1860
count
#delimit ;
local vars occdiffhh nonocchh constant;
foreach x of local vars{;
gen `x'wt = `x'*weight;
drop `x';
rename `x'wt `x';
};

#delimit ;
collapse (sum) occdiffhh nonocchh constant (mean)statefip1850 hasnormalschool, by(cty_fips1850 white1850);

#delimit cr

drop if white1850==0
gen constantrd = round(constant,1)
gen below10 = constantrd<10
bysort statefip hasnormalschool: egen totctytype = count(hasnormalschool)
bysort statefip hasnormalschool: egen totbelow10 = total(below10)
gen fracbelow10 = totbelow10/totctytype
bysort statefip: egen maxfracbelow10 = max(fracbelow10)

tab statefip if maxfracbelow10>=.5
drop if maxfracbelow10>=.5

drop if statefip==12|statefip==19|statefip==48|statefip==55

local vars occdiffhh 
foreach x of local vars{
gen `x'mean = `x'/(constant-nonocchh)
}

sort cty_fips

local vars occdiffhh
foreach x of local vars{

reghdfe `x'mean hasnormalschool if white1850==1, absorb(statefip1850) vce(robust)
gen sampoccpooled1850x = e(sample)==1

local vars2 sampoccpooled1850
foreach y of local vars2{
bysort cty_fips: egen `y' = max(`y'x)
}

drop if white1850==0 

keep cty_fips sampoccpooled1850
save sampoccpooled1850.dta, replace
}

restore


*****
*Sample for male occupations

preserve

drop if realprop_parents1850==.

keep if age1850>=16 & age1850<=18
keep if male1850==1
*Drop if dad's location not found
keep if occ1950_pop1850~=.

tab age1850
tab age1860
count
#delimit ;
local vars constant occdiffdad nonocc1860;
foreach x of local vars{;
gen `x'wt = `x'*weight;
drop `x';
rename `x'wt `x';
};

#delimit ;
collapse (sum) occdiffdad nonocc1860 constant  (mean)statefip1850 hasnormalschool, by(cty_fips1850 white1850);

#delimit cr

drop if white1850==0
gen constantrd = round(constant,1)
gen below10 = constantrd<10
bysort statefip hasnormalschool: egen totctytype = count(hasnormalschool)
bysort statefip hasnormalschool: egen totbelow10 = total(below10)
gen fracbelow10 = totbelow10/totctytype
bysort statefip: egen maxfracbelow10 = max(fracbelow10)

drop if maxfracbelow10>=.5

*Drop states for which 50% or more of their normal or asylum counties have sample size less than 10

drop if statefip==12|statefip==19|statefip==48|statefip==55

local vars occdiffdad
foreach x of local vars{
gen `x'mean = `x'/(constant-nonocc1860)
}

sort cty_fips
local vars occdiffdad
foreach x of local vars{

reghdfe `x'mean hasnormalschool if white1850==1, absorb(statefip1850) vce(robust)
gen sampoccmale1850x = e(sample)==1

local vars2 sampoccmale1850
foreach y of local vars2{
bysort cty_fips: egen `y' = max(`y'x)
}

drop if white1850==0 

keep cty_fips sampoccmale1850
save sampoccmale1850.dta, replace
}

restore

*Append datasets above to generate a dataset identifying samples by outcome

clear
use sampec11850.dta
merge 1:1 cty_fips using sampec21850.dta
rename _merge mergesampec1

merge 1:1 cty_fips using sampoccpooled1850.dta
rename _merge mergeoccpooled1850

merge 1:1 cty_fips using sampoccmale1850.dta
rename _merge mergeoccmale1850

keep cty_fips samp*

save sampwithoutpscore1850.dta, replace

*********************************

clear

*1850 Census
do "usa_00113.do"

***************************
*Merge 1850 Census to county crosswalk, and normal school/asylum counties

*Exclude people living in group quarters
tab gq
keep if gq~=3 & gq~=4

local vars occscore realprop
foreach x of local vars{
gen max`x'parent = max(`x'_mom, `x'_pop)
}

gen male1850 = sex==1
replace male1850 = . if sex==.

gen white1850 = race==1
replace white1850 = . if race==.

egen realprop_parents = rowtotal(realprop_pop realprop_mom), missing
gen nonmissingrealpropparents = realprop_parents~=.

* rename the variables to indicate the year they are from
for var histid realprop_parents age sex maxoccscoreparent race stateicp countyicp occ1950_mom occ1950_pop school occ1950 occscore perwt realprop_pop realprop_mom: rename X X1850

*Now merge with Eckert crosswalk
rename stateicp1850 icpsrst 
rename countyicp1850 icpsrcty
	
joinby icpsrst icpsrcty using "EGLP_1850.dta", unmatched(both)

rename _merge merge1850crosswalk

gen cty_fips = nhgisst_1990*100 + (nhgiscty_1990/10)

*Keep counties that VA independent cities were a part of
*https://www.bea.gov/system/files/methodologies/LAPI-Methodology.pdf
gen VAcounties = cty_fips==51177|cty_fips==51165|cty_fips==51121|cty_fips==51053

*For the VA counties replace the county fips of the surrounding county as the county of the independent city
*This will keep those records when merge to Normal Asylum counties
replace cty_fips = 51730 if cty_fips==51053
replace cty_fips = 51750 if cty_fips==51121
replace cty_fips = 51630 if cty_fips==51177
replace cty_fips = 51660 if cty_fips==51165

*Drop non-merged from crosswalk
drop if merge1850crosswalk==2 & VAcounties~=1

drop if merge1850crosswalk==1

merge m:1 cty_fips using "justnormasylum.dta"
rename _merge mergenormasylum

*Keep normal school and asylum counties
keep if mergenormasylum==3

rename cty_fips cty_fips1850

drop year nhgisst nhgiscty statenam nhgisnam area_base statenam_1990 nhgisnam_1990 icpsrst_1990 icpsrcty_1990 area us_state
for var hasnormalschool hasnormalorasylum mergenormasylum: rename X X1850

tostring cty_fips1850, gen(cty_fipstr)
gen statefip1850 = substr(cty_fipstr, 1,1) if length(cty_fipstr)==4
replace statefip1850 = substr(cty_fipstr, 1, 2) if length(cty_fipstr)==5
destring statefip1850, replace

*Keep states that did not have schools by 1860: AL, AR, AZ, CA, CO, FL, GA, IA, ID, IN, KS, 
*KY, LA, MD, ME, MN, MO, MS, MT, NC, ND, NE, NH, NM, OH, OK, OR, RI, SC, SD, TN, TX, UT, VA,
*VT, WA, WI, WV, and drop DE which did not have a normal school

#delimit ;
keep if statefip1850==1|statefip1850==5|statefip1850==4|statefip1850==6|statefip1850==8|
statefip1850==12|statefip1850==13|statefip1850==19|statefip1850==16|statefip1850==18|
statefip1850==20|statefip1850==21|statefip1850==22|statefip1850==24|statefip1850==23|
statefip1850==27|statefip1850==29|statefip1850==28|statefip1850==30|statefip1850==37|
statefip1850==38|statefip1850==31|statefip1850==33|statefip1850==35|statefip1850==39|statefip1850==40|
statefip1850==41|statefip1850==44|statefip1850==45|statefip1850==46|statefip1850==47|
statefip1850==48|statefip1850==49|statefip1850==51|statefip1850==50|
statefip1850==53|statefip1850==55|statefip1850==54;

#delimit cr
*Drop states that were not yet states on Census Day 1850: 

#delimit ;
drop if statefip1850==6|statefip1850==27|statefip1850==38|statefip1850==46|statefip1850==49|statefip==4|
statefip==8|statefip==16|statefip==30|statefip==31|statefip==32|statefip==35|statefip==40|
statefip==41|statefip==53|statefip==56;

#delimit cr

*Drop DE, Georgia, RI, SC because no asylum counties

drop if statefip==13|statefip==44|statefip==45

tab statefip

save "1850_small_diffmerge.dta", replace

*Merging the IPUMS censuses to the Census Tree crosswalks

clear

use "1850_1860.dta", replace

merge 1:m histid1850 using "1850_small_diffmerge.dta"
rename _merge mergetree1850

gen merged1850 = mergetree1850==3
drop if mergetree1850==1

gen school1850U = school1850==2

gen black1850 = race1850==2
replace black1850 = . if race1850==.

gen dad_unsk = occ1950_pop1850>=700 & occ1950_pop1850<=970
replace dad_unsk = . if (occ1950_pop1850==.|occ1950_pop1850==999)

gen posoccdad = (occ1950_pop1850~=. & occ1950_pop1850~=999)

gen cty_fips = cty_fips1850

keep if nonmissingrealpropparents==1

gen head_pop = relate_pop==1

gen head_mom = relate_mom==1

gen livewithpop = occ1950_pop1850~=.
gen livewithmom = occ1950_mom1850~=.

gen liveinbirthstate_pop = bpl_pop==statefip

gen liveinbirthstate_mom = bpl_mom == statefip

gen liveinbirthstate = bpl==statefip
replace liveinbirthstate=. if bpl==.

gen foreignborn = bpl>=100
replace foreignborn = . if bpl==.

gen foreignborn_pop = bpl_pop>=100

gen foreignborn_mom = bpl_mom>=100

gen famunit_1 = famunit==1
replace famunit_1 = . if famunit==.

replace age_pop = 0 if age_pop==.
replace age_mom = 0 if age_mom==.

*Merge to 1860 record
merge m:1 histid1860 using "1860_small.dta"
rename _merge mergetree1860
drop if mergetree1860==2

gen merge50and60 = mergetree1850==3 & mergetree1860==3

egen taghistid1850 = tag(histid1850)

*Merge to samples without pscores
merge m:1 cty_fips1850 using sampwithoutpscore1850.dta
rename _merge mergesampwithoutpscore

#delimit cr

*Table A18: Differences in selection into merging for the states in our main analysis
gen samp = 1 if realprop_parents1850~=. & (statefip~=12|statefip~=19|statefip~=48|statefip~=55) & age1850>=16 & age1850<=18

#delimit ;

local control male1850 maxoccscoreparent1850 age_mom age_pop head_pop head_mom livewithpop livewithmom  
liveinbirthstate liveinbirthstate_pop liveinbirthstate_mom foreignborn foreignborn_pop foreignborn_mom nsibs famunit_1;
 
#delimit cr

*White children
reghdfe merge50and60 `control' if  white1850==1 & realprop_parents1850<=150 & samp==1 & sampec1white==1 [pw=weight], absorb(cty_fips1850) cluster(cty_fips1850)
su merge50and60 if e(sample)==1
local mean = r(mean)
su `control' if e(sample)==1
outreg2 using selection_1850.xls, replace excel dec(3) addstat(dvmean, `mean') adec(3)

reghdfe merge50and60 `control' if  white1850==1 & realprop_parents1850>150  & samp==1 & sampec2white==1 [pw=weight], absorb(cty_fips1850) cluster(cty_fips1850)
su merge50and60 if e(sample)==1
local mean = r(mean)
outreg2 using selection_1850.xls, append excel dec(3) addstat(dvmean, `mean') adec(3)

*Propensity score estimation: separately for each subsample

gen samppscore = 1 if realprop_parents1850~=. & age1850>=16 & age1850<=18


#delimit ;

local control male1850 maxoccscoreparent1850 age_mom age_pop head_pop head_mom livewithpop livewithmom  
liveinbirthstate liveinbirthstate_pop liveinbirthstate_mom foreignborn foreignborn_pop foreignborn_mom nsibs famunit_1;
 
#delimit cr
probit merge50and60 `control' if  white1850==1 & realprop_parents1850<=150  & samppscore==1 & sampec1white==1 [pw=weight]
distinct cty_fips1850 if e(sample)==1
predict predictwhitebmed1, pr

probit merge50and60 `control' if  white1850==1 & realprop_parents1850>150  & samppscore==1 & sampec2white==1 [pw=weight]
distinct cty_fips1850 if e(sample)==1
predict predictwhitebmed0, pr

*Propensity score estimation for the occupation specifications, some of which are just by sex

#delimit cr
probit merge50and60 `control' if  white1850==1 & male1850==1  & samppscore==1 & sampoccmale==1 [pw=weight]
distinct cty_fips1850 if e(sample)==1
predict predictwhitemale, pr

probit merge50and60 `control' if  white1850==1 & male1850==0  & samppscore==1 & sampoccmale==1 [pw=weight]
distinct cty_fips1850 if e(sample)==1
predict predictwhitefemale, pr

#delimit cr
probit merge50and60 `control' if  white1850==1  & samppscore==1 & sampoccpooled==1 [pw=weight]
distinct cty_fips1850 if e(sample)==1
predict predictwhite, pr

#delimit ;
local vars predictwhitebmed1 predictwhitebmed0 
predictwhitemale predictwhitefemale predictwhite;

#delimit cr
foreach x of local vars{
gen ps`x' = 1/`x'
}


save "1850_1860_diffmergeall.dta", replace

keep if merge50and60==1

gen ps = pspredictwhitebmed1 if white1850==1 & realprop_parents1850<=150 & sampec1white==1
replace ps = pspredictwhitebmed0 if white1850==1 & realprop_parents1850>150 & sampec2white==1

gen weightwithps = weight*ps

gen psoccmobpooled = pspredictwhite if white1850==1 & sampoccpooled==1

gen weightwithpsoccmobpooled = weight*psoccmobpooled

gen psoccmobbysex = pspredictwhitemale if white1850==1 & male1850==1 & sampoccmale==1
replace psoccmobbysex = pspredictwhitefemale if white1850==1 & male1850==0 & sampoccmale==1

gen weightwithpsoccmobbysex = weight*psoccmobbysex

local vars school1860
foreach x of local vars{
gen `x'U = `x'==2
}

gen constant = 1

gen unsk1860 = occ19501860>=700 & occ19501860<=970
replace unsk1860 = . if (occ19501860==.|occ19501860==999)

gen unsk_sp1860 = occ1950_sp1860>=700 & occ1950_sp1860<=970
replace unsk_sp1860 = . if (occ1950_sp1860==.|occ1950_sp1860==999)

gen farmer1860hh = occ19501860==100|occ19501860==123|occ1950_sp1860==100|occ1950_sp1860==123
replace farmer1860hh = . if ((occ19501860==.) & (occ1950_sp1860==.))

gen farmer1860 = occ19501860==100|occ19501860==123

gen whitecollar1860 = (occ19501860>=0 & occ19501860<=99)|(occ19501860>=200 & occ19501860<=490)
gen whitecollar_sp1860 = (occ1950_sp1860>=0 & occ1950_sp1860<=99)|(occ1950_sp1860>=200 & occ1950_sp1860<=490)

gen craftoper1860 = occ19501860>=500 & occ19501860<=690
gen craftoper_sp1860 = occ1950_sp1860>=500 & occ1950_sp1860<=690

gen nonocc1860 = occ19501860>=980 & occ19501860<=999

gen nonocc_sp1860 = occ1950_sp1860>=980 & occ1950_sp1860<=999

gen occschool1860 = occ19501860==983

local vars unsk craftoper whitecollar
foreach x of local vars{
egen `x'hh = rowmax(`x'1860 `x'_sp1860)
replace `x'hh = . if occ19501860==. & occ1950_sp1860==.
}

gen nonocchh = nonocc1860==1 & nonocc_sp1860==1
replace nonocchh = 1 if nonocc1860==1 & occ1950_sp1860==.
replace nonocchh = . if occ19501860==. & occ1950_sp1860==.

gen dadunsk1850 = occ1950_pop1850>=700 & occ1950_pop1850<=970
gen momunsk1850 = occ1950_mom1850>=700 & occ1950_mom1850<=970

gen dadwhitecollar1850 = (occ1950_pop1850>=0 & occ1950_pop1850<=99)|(occ1950_pop1850>=200 & occ1950_pop1850<=490)
gen momwhitecollar1850 = (occ1950_mom1850>=0 & occ1950_mom1850<=99)|(occ1950_mom1850>=200 & occ1950_mom1850<=490)

gen dadcraftoper1850 = occ1950_pop1850>=500 & occ1950_pop1850<=690
gen momcraftoper1850 = occ1950_mom1850>=500 & occ1950_mom1850<=690

gen dadnonocc1850 = occ1950_pop1850>=980 & occ1950_pop1850<=999
gen momnonocc1850 = occ1950_mom1850>=980 & occ1950_mom1850<=999

gen parent_farmer = occ1950_pop1850==100|occ1950_pop1850==123|occ1950_mom1850==100|occ1950_mom1850==123
replace parent_farmer = . if ((occ1950_pop1850==.) & (occ1950_mom1850==.))

gen dad_farmer = occ1950_pop1850==100|occ1950_pop1850==123

local vars unsk craftoper whitecollar 
foreach x of local vars{
egen parent`x' = rowmax(dad`x'1850 mom`x'1850)
replace parent`x' = . if occ1950_pop1850==. & occ1950_mom1850==.
}

gen occdiffhh = unskhh==1 & parentunsk~=1
replace occdiffhh = 1 if craftoperhh==1 & parentcraftoper~=1
replace occdiffhh = 1 if whitecollarhh==1 & parentwhitecollar~=1
replace occdiffhh = 1 if farmer1860hh==1 & parent_farmer~=1

gen occdiffdad = unsk1860==1 & dadunsk~=1
replace occdiffdad = 1 if craftoper1860==1 & dadcraftoper~=1
replace occdiffdad = 1 if whitecollar1860==1 & dadwhitecollar~=1
replace occdiffdad = 1 if farmer1860==1 & dad_farmer~=1

gen parentnonocc = dadnonocc1850==1 & momnonocc1850==1
replace parentnonocc = 1 if dadnonocc1850==1 & occ1950_mom1850==.
replace parentnonocc = 1 if momnonocc1850==1 & occ1950_pop1850==.

gen married1860 = school_sp1860~=. 
 
egen hhrealprop = rowtotal(realprop1860 realprop_sp1860), missing
egen hhpersprop = rowtotal(persprop1860 persprop_sp1860), missing

gen pargroup2 = 1  if realprop_parents1850<=150
replace pargroup2 = 2 if realprop_parents1850>150

*Generate top quartile indicators for real estate and personal estate
gen hhrealpropgteq400 = hhrealprop>=400
gen hhperspropgteq372 = hhpersprop>=372

gen parzero150 = realprop_parents1850<=150
gen par1501000 = realprop_parents1850>150 & realprop_parents1850<=1000
gen pargt1000 = realprop_parents1850>1000

save "1850_1860_diffmerged.dta", replace

set seed 834138

*Table A19 columns 3-5, A23: Outcomes by Parental Real Estate

forvalues i = 1(1)2{
preserve


keep if pargroup2==`i'
drop if realprop_parents1850==.

keep if age1850>=16 & age1850<=18

tab age1850
tab age1860
count
#delimit ;
local vars occschool1860 hhrealpropgteq400 hhperspropgteq372 male1850 school1860U constant married1860;
foreach x of local vars{;
gen `x'wt = `x'*weightwithps;
drop `x';
rename `x'wt `x';
};

#delimit ;
collapse (sum) occschool1860 hhrealpropgteq400 hhperspropgteq372 school1860U male1850 constant married1860 (mean)statefip1850 hasnormalschool, by(cty_fips1850 white1850);

#delimit cr

drop if white1850==0

gen constantrd = round(constant,1)
gen below10 = constantrd<10
bysort statefip hasnormalschool: egen totctytype = count(hasnormalschool)
bysort statefip hasnormalschool: egen totbelow10 = total(below10)
gen fracbelow10 = totbelow10/totctytype
bysort statefip: egen maxfracbelow10 = max(fracbelow10)

*Drop states for which 50% or more of their normal or asylum counties have sample size less than 10

drop if maxfracbelow10>=.5

local vars occschool1860 hhrealpropgteq400 hhperspropgteq372  male1850 married1860 school1860U
foreach x of local vars{
gen `x'mean = `x'/constant
}

sort cty_fips

local vars occschool1860 hhrealpropgteq400 hhperspropgteq372 male1850 school1860U married1860   
foreach x of local vars{

reghdfe `x'mean hasnormalschool if white1850==1, absorb(statefip1850) vce(robust)
su `x'mean if e(sample)==1 & hasnormalschool==0
local mean = r(mean)
su constant if e(sample)==1, d
tab statefip1850 if e(sample)==1
outreg2 using censustree18501860ps.xls, append excel dec(3) addstat(dvmean, `mean') adec(3)

#delimit ;
permute hasnormalschool _b[hasnormalschool], strata(statefip1850) reps(1000): reg `x'mean hasnormalschool i.statefip1850 if white1850==1;

#delimit cr
}


pca occschool1860mean hhrealpropgteq400mean hhperspropgteq372mean school1860Umean married1860mean if white1850==1
predict pc1 pc2 pc3 pc4 pc5, score
reghdfe pc1 hasnormalschool1850 if white1850==1, absorb(statefip1850) vce(robust)
su pc1 if e(sample)==1 & hasnormalschool1850==0
local mean = r(mean)
outreg2 using censustree18501860ps.xls, append excel dec(3) addstat(dvmean, `mean') adec(3)

su pc1 if white1850==1

#delimit ;
permute hasnormalschool1850 _b[hasnormalschool1850], strata(statefip1850) reps(1000): reg pc1 hasnormalschool1850 i.statefip1850 if white1850==1;

#delimit cr

restore
}

********************************
*Tables A20, A22: Occupations, and parental real estate distribution
********************************

preserve

drop if realprop_parents1850==.

keep if age1850>=16 & age1850<=18

tab age1850
tab age1860
count
#delimit ;
local vars occdiffhh parzero150 par1501000 pargt1000 unskhh craftoperhh whitecollarhh nonocchh farmer1860hh male1850 parentunsk parentcraftoper parentwhitecollar parentnonocc parent_farmer constant;
foreach x of local vars{;
gen `x'wt = `x'*weightwithpsoccmobpooled;
drop `x';
rename `x'wt `x';
};

#delimit ;
collapse (sum) occdiffhh parzero150 par1501000 pargt1000 unskhh craftoperhh whitecollarhh nonocchh farmer1860hh parentunsk parentcraftoper parentwhitecollar parentnonocc parent_farmer male1850 constant (mean)statefip1850 hasnormalschool, by(cty_fips1850 white1850);

#delimit cr

drop if white1850==0
gen constantrd = round(constant,1)
gen below10 = constantrd<10
bysort statefip hasnormalschool: egen totctytype = count(hasnormalschool)
bysort statefip hasnormalschool: egen totbelow10 = total(below10)
gen fracbelow10 = totbelow10/totctytype
bysort statefip: egen maxfracbelow10 = max(fracbelow10)

drop if maxfracbelow10>=.5

local vars  parzero150 par1501000 pargt1000 parent_farmer parentunsk parentcraftoper parentwhitecollar parentnonocc unskhh craftoperhh whitecollarhh nonocchh farmer1860hh male1850 
foreach x of local vars{
gen `x'mean = `x'/constant
}

local vars occdiffhh 
foreach x of local vars{
gen `x'mean = `x'/(constant-nonocchh)
}

sort cty_fips

local vars occdiffhh parzero150 par1501000 pargt1000 unskhh craftoperhh whitecollarhh nonocchh  farmer1860hh male1850 parentunsk parentcraftoper parentwhitecollar  parentnonocc parent_farmer  
foreach x of local vars{

reghdfe `x'mean hasnormalschool if white1850==1, absorb(statefip1850) vce(robust)
su `x'mean if e(sample)==1 & hasnormalschool==0
local mean = r(mean)
su constant if e(sample)==1, d
tab statefip1850 if e(sample)==1
outreg2 using censustree18501860ps.xls, append excel dec(3) addstat(dvmean, `mean') adec(3)

#delimit ;
permute hasnormalschool _b[hasnormalschool], strata(statefip1850) reps(1000): reg `x'mean hasnormalschool i.statefip1850 if white1850==1;

#delimit cr
}
restore


*****
*Table A19 column 6, A21: Male occupations

preserve

drop if realprop_parents1850==.

keep if age1850>=16 & age1850<=18
keep if male1850==1
*Drop if dad's location not found
keep if occ1950_pop1850~=.

tab age1850
tab age1860
count
#delimit ;
local vars constant occdiffdad unsk1860 craftoper1860 whitecollar1860 farmer1860 nonocc1860 dadunsk1850 dadcraftoper1850 dadwhitecollar1850 dad_farmer dadnonocc1850  ;
foreach x of local vars{;
gen `x'wt = `x'*weightwithpsoccmobbysex;
drop `x';
rename `x'wt `x';
};

#delimit ;
collapse (sum) occdiffdad unsk1860 craftoper1860 whitecollar1860 farmer1860 nonocc1860 dadunsk1850 dadcraftoper1850 dadwhitecollar1850 dad_farmer dadnonocc1850 constant  (mean)statefip1850 hasnormalschool, by(cty_fips1850 white1850);

#delimit cr

drop if white1850==0
gen constantrd = round(constant,1)
gen below10 = constantrd<10
bysort statefip hasnormalschool: egen totctytype = count(hasnormalschool)
bysort statefip hasnormalschool: egen totbelow10 = total(below10)
gen fracbelow10 = totbelow10/totctytype
bysort statefip: egen maxfracbelow10 = max(fracbelow10)

drop if maxfracbelow10>=.5

local vars   unsk1860 craftoper1860 whitecollar1860 farmer1860 nonocc1860 dadunsk1850 dadcraftoper1850 dadwhitecollar1850 dad_farmer dadnonocc1850
foreach x of local vars{
gen `x'mean = `x'/constant
}

local vars occdiffdad
foreach x of local vars{
gen `x'mean = `x'/(constant-nonocc1860)
}

sort cty_fips
local vars occdiffdad unsk1860 craftoper1860 whitecollar1860 farmer1860 nonocc1860 dadunsk1850 dadcraftoper1850 dadwhitecollar1850 dad_farmer dadnonocc1850  
foreach x of local vars{

reghdfe `x'mean hasnormalschool if white1850==1, absorb(statefip1850) vce(robust)
su `x'mean if e(sample)==1 & hasnormalschool==0
local mean = r(mean)
su constant if e(sample)==1, d
tab statefip1850 if e(sample)==1

outreg2 using censustree18501860ps.xls, append excel dec(3) addstat(dvmean, `mean') adec(3)

#delimit ;
di "`x'";
permute hasnormalschool _b[hasnormalschool], strata(statefip1850) reps(1000): reg `x'mean hasnormalschool i.statefip1850 if white1850==1;
#delimit cr
}

restore
